#ifndef DATA_TRANSFER_H
#define DATA_TRANSFER_H

//http://digilander.libero.it/robang/rubrica/serial.htm
//http://www.ibm.com/developerworks/cn/linux/l-serials/index.html

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

class data_transfer
{
private:
	int fd;

	//initialize
	void init();
public:
	data_transfer();
	virtual ~data_transfer();

	//if tty is open,return true;else return false
	virtual bool is_open();

	//send data
	//param pbuf points to the data prepared to be sent
	//param size is the data size in bytes
	//return -1 if sending failed,else return the number
	//of bytes sent
	int sendTty(unsigned char *pbuf,int size);

	//receive data
	//param pbuf points to where the recevied data will be stored
	//param size is the buffer size in bytes
	//return -1 if receving failed,else return the number 
	//of bytes receives
	int recvTty(unsigned char *pbuf,int size);

	//clear the input buffer
	void flush();

	virtual int send(unsigned char *pbuf,int size,bool echo=true)=0;
	virtual int recv(unsigned char *pbuf,int size,bool echo=true)=0;
};

#endif
